<template>
    <view class="maincontent">
        <image class="img-bg" :src="imgBg"></image>
        <page-head  :transparent="imgBg" :fixed="true">
            <slot slot="title">
                <image class="img-title" :src="imgTilte"/>
            </slot>
            <swiper-recommend slot="bottom" :current="current" @onClick="changCurrent" :list="cids"/>
        </page-head>
        <goods-list :goods-list="goodsList"/>
    </view>
</template>

<script>
    import SwiperRecommend from './components/swiper-recommend'
    import GoodsList from '../../components/tb-goods-list2'
    import imgTilte from './img/faxianhaohuo.png'
    import imgBg from './img/purpleBg.png'

    let page = 1,
        pageSize = 20;
    const cids = ["全部",
        "女装",
        "男装",
        "内衣",
        "母婴",
        "化妆品",
        "居家",
        "鞋包配饰",
        "美食",
        "文体车品",
        "数码家电"];

    export default {
        components: {
            SwiperRecommend,
            GoodsList
        },
        onReady() {
			this.genCommon();
            this.getData()
        },

        onLoad(event) {

        },
        data() {
            return {
                current: 0,
                pageIndex: page,
                imgTilte,
                imgBg,
                goodsList: [],
                cids: cids
            };
        },
        onReachBottom: function () {
            this.getData(this.pageIndex);
        },
        onPullDownRefresh: function () {
            uni.stopPullDownRefresh(); //停止下拉刷新动画
            this.getData();
        },
        methods: {
			genCommon() {
				const that = this
				let url = window.location.href;
				console.log("url===" + url);
				let reg_url = /^[^\?]+\?([\w\W]+)$/,
					reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
					arr_url = reg_url.exec(url),
					ret = {};
				if (arr_url && arr_url[1]) {
					let str_para = arr_url[1],
						result;
					while ((result = reg_para.exec(str_para)) != null) {
						ret[result[1]] = result[2];
					}
				}
				let token = ret.token;
				console.log("token==" + token);
				if (token != '' && token != 'null' && token != null && token != undefined && token != 'undefined') {
					uni.setStorageSync('token', decodeURI(token));
					uni.setStorageSync('bump', ret.bump);
					uni.setStorageSync('relationId', ret.relationId);
					uni.setStorageSync('mobile', ret.customerId);
				}
			},
            changCurrent(index) {
                this.current = index;
                this.getData()
            },
            //获取数据
            async getData(pageIndex = page) {
                const data = await this.$ajax.post(this.$usereq + '/coupon-product/home/bestSelling/goods', {
                    cid: this.current || 0,
                    "pageno": pageIndex,
                    "pagesize": pageSize
                },'.loading-text');
                if (data) {
                    const mock = data.data;
                    if (pageIndex > page) {
                        this.goodsList = [...this.goodsList, ...mock];
                    } else {
                        this.goodsList = mock;
                    }
                    this.pageIndex = pageIndex + 1
                }
            },
        }
    };
</script>

<style lang="scss">
    .maincontent {
        .img-bg {
            position: fixed;
            width: 750px;
            height: 1108px;
            top: 0;
        }

        .img-title {
            width: 160px;
            height: 46px;
            display: block;
        }
    }
</style>
